package edu.ajou.scw;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Team {

	private String id;
	private String leader;
	private String type;
	private ArrayList<String> member;
	private ArrayList<String> slides;
	
	public Team(String _id, String _leader, String _type)
	{
		id = _id;
		leader = _leader;
		type = _type;
		member = new ArrayList<String>();
		slides = new ArrayList<String>();
	}
	
	public static Team Load(String _id)
	{
		DatabaseConnector conn = new DatabaseConnector();
		
		return conn.getTeamById(_id);
	}
	
	public void addMember(String memberId)
	{
		member.add(memberId);
	}
	
	public void parseMembers(String memberStr)
	{
		String[] members = memberStr.split(";");
		
		for (int i=0; i<members.length; i++)
		{
			if (!members[i].equals(""))
				member.add(members[i]);
		}
	}
	
	public void parseSlides(String slideStr)
	{
		String[] slideList = slideStr.split(";");

		for (int i=0; i<slideList.length; i++)
		{
			if (!slideList[i].equals(""))
				slides.add(slideList[i]);
		}
	}
	
	
	public void setSlides(ArrayList<String> slideArr)
	{
		slides = slideArr;
	}
	
	public String getId()
	{
		return id;
	}
	
	public String getType()
	{
		return type;
	}
	
	public ArrayList<String> getMembers()
	{
		return member;
	}
	
	public String getMemberStr()
	{
		String list = "";
		
		for (int i=0; i<member.size(); i++)
			list += member.get(i) + ";";
		
		return list;
	}
	
	public String getSlideStr()
	{
		String list = "";
		
		for (int i=0; i<slides.size(); i++)
			list += slides.get(i) + ";";
		
		return list;
	}
	
	public void putIntoDb()
	{
		DatabaseConnector conn = new DatabaseConnector();
		
		String sql = String.format("insert into `team_info` (`team_id`, `leader`, `type`, `members`, `slides`) values (\"%s\", \"%s\", \"%s\", \"%s\", \"%s\")", 
				id, leader, type, getMemberStr(), getSlideStr());
		conn.query(sql);
	}
	
	public void updateDb()
	{
		DatabaseConnector conn = new DatabaseConnector();
		
		String sql = String.format("update `team_info` set `leader` = \"%s\", `type` = \"%s\", `members` = \"%s\", `slides` = \"%s\" where `team_id` = \"%s\"", 
				leader, type, getMemberStr(), getSlideStr(), id);
		conn.query(sql);
	}
	
	@SuppressWarnings("unchecked")
	public String getJSONString()
	{		
		JSONObject obj = new JSONObject();
		
		obj.put("id", id);
		obj.put("leader", leader);
		obj.put("type", type);
		JSONArray memberArr = new JSONArray();
		
		for (int i=0; i<member.size(); i++)
			memberArr.add(member.get(i));
		
		obj.put("members", memberArr);
		
		JSONArray slideArr = new JSONArray();
		for (int i=0; i<slides.size(); i++)
		{
			slideArr.add(slides.get(i));
		}
		obj.put("pages", slides.size());
		obj.put("slides", slideArr);
		
		return obj.toJSONString();
	}
	
	public void invite(String newMember)
	{
		DatabaseConnector conn = new DatabaseConnector();
		PushSender sender = new PushSender();

		JSONArray newMemberArr = new JSONArray();
		
		ArrayList<String> oldMemberList = (ArrayList<String>)member.clone();
		
		ArrayList<UserInfo> uil = conn.getUserInfo(newMember, true);
		if (uil.size() > 0)
		{
			member.add(newMember);
			updateDb();
				
			String inviteMessage = getJSONString();
			sender.sendPush(newMember, "team_info", inviteMessage);
				
			newMemberArr.add(newMember);
			
			JSONObject obj = new JSONObject();
			obj.put("team_id", this.id);
			obj.put("action", "newMember");
			obj.put("members", newMemberArr);
			
			String notice = obj.toJSONString();
			
			for (int i=0; i<oldMemberList.size(); i++)
			{
				sender.sendPush(oldMemberList.get(i), "action", notice);
			}
		}
	}
	
	public void invite(String[] idList)
	{
		DatabaseConnector conn = new DatabaseConnector();
		PushSender sender = new PushSender();

		JSONArray newMemberArr = new JSONArray();
		
		ArrayList<String> oldMemberList = (ArrayList<String>)member.clone();
		
		for (int i=0; i<idList.length; i++)
		{
			String newMember = idList[i];
			ArrayList<UserInfo> uil = conn.getUserInfo(newMember, true);
			if (uil.size() > 0)
			{
				member.add(newMember);
				updateDb();
				
				String inviteMessage = getJSONString();
				sender.sendPush(newMember, "team_info", inviteMessage);
				
				newMemberArr.add(newMember);
			}
		}
		
		if (newMemberArr.size() > 0)
		{
			JSONObject obj = new JSONObject();
			obj.put("team_id", this.id);
			obj.put("action", "newMember");
			obj.put("members", newMemberArr);
			
			String notice = obj.toJSONString();
			
			for (int i=0; i<oldMemberList.size(); i++)
			{
				sender.sendPush(oldMemberList.get(i), "member", notice);
			}
		}
	}
	
	public void reject(String memberId)
	{
		boolean success = false;
		
		for (int i=0; i<member.size(); i++)
		{
			String cur = member.get(i);
			
			if (cur.equals(memberId))
			{
				member.remove(i);
				success = true;
				break;
			}
		}
		
		if (success)
		{
			updateDb();
			
			PushSender sender = new PushSender();
			JSONObject obj = new JSONObject();
			obj.put("team_id", this.id);
			obj.put("action", "rejectMember");
			obj.put("member", memberId);
			
			String rejectMessage = obj.toJSONString();
			
			sender.sendPush(memberId, "member", rejectMessage);
			
			for (int i=0; i<member.size(); i++)
			{
				sender.sendPush(member.get(i), "action", rejectMessage);
			}
		}
	}
	
	public void inviteAll()
	{
		String message = getJSONString();
		PushSender sender = new PushSender();
		
		for (int i=0; i<member.size(); i++)
		{
			String id = member.get(i);
			sender.sendPush(id, "team_info", message);
		}
	}
	
	public void closeTeam()
	{
		String message = "{ \"team_id\": \"" + id + "\", \"action\": \"closed\" }";
		PushSender sender = new PushSender();
		
		for (int i=0; i<member.size(); i++)
		{
			String id = member.get(i);
			sender.sendPush(id, "team_info", message);
		}
	}
}
